/* Copyright (c) 2006, 2009, Carl Burch. License information is located in the
 * com.cburch.logisim.Main source code and at www.cburch.com/logisim/. */
 
package com.cburch.logisim.util;

import java.awt.Event;
import java.awt.event.InputEvent;
import java.util.ArrayList;
import java.util.StringTokenizer;
import java.util.Iterator;

public class InputEventUtil {
    public static String CTRL    = "Ctrl";
    public static String SHIFT   = "Shift";
    public static String ALT     = "Alt";
    public static String BUTTON1 = "Button1";
    public static String BUTTON2 = "Button2";
    public static String BUTTON3 = "Button3";

    private InputEventUtil() { }

    public static int fromString(String str) {
        int ret = 0;
        StringTokenizer toks = new StringTokenizer(str);
        while(toks.hasMoreTokens()) {
            String s = toks.nextToken();
            if(s.equals(CTRL))          ret |= InputEvent.CTRL_DOWN_MASK;
            else if(s.equals(SHIFT))    ret |= InputEvent.SHIFT_DOWN_MASK;
            else if(s.equals(ALT))      ret |= InputEvent.ALT_DOWN_MASK;
            else if(s.equals(BUTTON1))  ret |= InputEvent.BUTTON1_DOWN_MASK;
            else if(s.equals(BUTTON2))  ret |= InputEvent.BUTTON2_DOWN_MASK;
            else if(s.equals(BUTTON3))  ret |= InputEvent.BUTTON3_DOWN_MASK;
            else throw new NumberFormatException("InputEventUtil");
        }
        return ret;
    }

    public static String toString(int mods) {
        ArrayList arr = new ArrayList();
        if((mods & InputEvent.CTRL_DOWN_MASK)    != 0) arr.add(CTRL);
        if((mods & InputEvent.ALT_DOWN_MASK)     != 0) arr.add(ALT);
        if((mods & InputEvent.SHIFT_DOWN_MASK)   != 0) arr.add(SHIFT);
        if((mods & InputEvent.BUTTON1_DOWN_MASK) != 0) arr.add(BUTTON1);
        if((mods & InputEvent.BUTTON2_DOWN_MASK) != 0) arr.add(BUTTON2);
        if((mods & InputEvent.BUTTON3_DOWN_MASK) != 0) arr.add(BUTTON3);

        if(arr.isEmpty()) return "";

        StringBuffer ret = new StringBuffer();
        Iterator it = arr.iterator();
        ret.append((String) it.next());
        while(it.hasNext()) {
            ret.append(" ");
            ret.append((String) it.next());
        }
        return ret.toString();
    }

    public static int fromDisplayString(String str) {
        int ret = 0;
        StringTokenizer toks = new StringTokenizer(str);
        while(toks.hasMoreTokens()) {
            String s = toks.nextToken();
            if(s.equals(Strings.get("ctrlMod", Strings.utilSource)))            ret |= InputEvent.CTRL_DOWN_MASK;
            else if(s.equals(Strings.get("altMod", Strings.utilSource)))        ret |= InputEvent.ALT_DOWN_MASK;
            else if(s.equals(Strings.get("shiftMod", Strings.utilSource)))      ret |= InputEvent.SHIFT_DOWN_MASK;
            else if(s.equals(Strings.get("button1Mod", Strings.utilSource)))    ret |= InputEvent.BUTTON1_DOWN_MASK;
            else if(s.equals(Strings.get("button2Mod", Strings.utilSource)))    ret |= InputEvent.BUTTON2_DOWN_MASK;
            else if(s.equals(Strings.get("button3Mod", Strings.utilSource)))    ret |= InputEvent.BUTTON3_DOWN_MASK;
            else throw new NumberFormatException("InputEventUtil");
        }
        return ret;
    }

    public static String toDisplayString(int mods) {
        ArrayList arr = new ArrayList();
        if((mods & InputEvent.CTRL_DOWN_MASK)    != 0) arr.add(Strings.get("ctrlMod", Strings.utilSource));
        if((mods & InputEvent.ALT_DOWN_MASK)     != 0) arr.add(Strings.get("altMod", Strings.utilSource));
        if((mods & InputEvent.SHIFT_DOWN_MASK)   != 0) arr.add(Strings.get("shiftMod", Strings.utilSource));
        if((mods & InputEvent.BUTTON1_DOWN_MASK) != 0) arr.add(Strings.get("button1Mod", Strings.utilSource));
        if((mods & InputEvent.BUTTON2_DOWN_MASK) != 0) arr.add(Strings.get("button2Mod", Strings.utilSource));
        if((mods & InputEvent.BUTTON3_DOWN_MASK) != 0) arr.add(Strings.get("button3Mod", Strings.utilSource));

        if(arr.isEmpty()) return "";

        StringBuffer ret = new StringBuffer();
        Iterator it = arr.iterator();
        ret.append((String) it.next());
        while(it.hasNext()) {
            ret.append(" ");
            ret.append((String) it.next());
        }
        return ret.toString();
    }

    public static String toKeyDisplayString(int mods) {
        ArrayList arr = new ArrayList();
        if((mods & Event.META_MASK)  != 0) arr.add(Strings.get("metaMod", Strings.utilSource));
        if((mods & Event.CTRL_MASK)  != 0) arr.add(Strings.get("ctrlMod", Strings.utilSource));
        if((mods & Event.ALT_MASK)   != 0) arr.add(Strings.get("altMod", Strings.utilSource));
        if((mods & Event.SHIFT_MASK) != 0) arr.add(Strings.get("shiftMod", Strings.utilSource));

        if(arr.isEmpty()) return "";

        StringBuffer ret = new StringBuffer();
        Iterator it = arr.iterator();
        ret.append((String) it.next());
        while(it.hasNext()) {
            ret.append(" ");
            ret.append((String) it.next());
        }
        return ret.toString();
    }
}
